package com.zhoug.ocr.textin;

import android.app.Application;

import com.zhoug.common.content.SharedPreferencesWrap;

/**
 * @Author: zhoug
 * @Date: 2023/3/6
 * @Description:
 */
public class TextInConfig {
    private static Application application;

    public static final String X_TI_APP_ID_KEY = "x-ti-app-id";
    public static String x_ti_app_id = "dfe852aab25a3e1f5b7b753db7f52e9f";
    public static final String X_TI_SECRET_CODE_KEY = "x-ti-secret-code";
    public static String x_ti_secret_code = "a723f6c6ed91846fede6a8d2885685a1";


    public static final String TEXTIN_BASE_URL = "https://api.textin.com/";
    //表格识别
    private static final String TEXTIN_API_TABLE = TEXTIN_BASE_URL + "ai/service/v2/recognize/table";
    //增值税发票识别
    private static final String TEXTIN_API_VAT_INVOICE = TEXTIN_BASE_URL + "robot/v1.0/api/vat_invoice";
    //营业执照识别
    private static final String TEXTIN_API_BUSINESS_LICENSE = TEXTIN_BASE_URL + "robot/v1.0/api/business_license";
    //国内通用票据识别:
    //包括增值税普通发票、增值税普通发票（卷票）、增值税专用发票、增值税电子专用发票、增值税电子普通发票、
    // 货物运输业增值税专用发票、机动车销售统一发票、二手车销售统一发票、通用机打发票、通用定额发票、旅客运输普票、公路客运发票、
    // 船运客票、出租车发票、停车费发票、过路过桥费发票、教育费收据、行程单、火车票、增值税销货清单和其他可报销票据
    private static final String TEXTIN_API_BILL = TEXTIN_BASE_URL + "robot/v1.0/api/bills_crop";
    //表格识别
    private static String tableApi = "http://192.168.0.199:10002/icr/recognize_multi_table_raw";
    //增值税发票识别,使用国内通用票据识别接口
    private static String vatInvoiceApi = "http://192.168.0.199:10001/cci_ai/service/v1/receipt_crop_and_recog";
    //营业执照识别
    private static String businessLicenseApi = "http://192.168.0.199:10004/cci_ai/service/v1/business_license";
    //国内通用票据识别
    //包括增值税普通发票、增值税普通发票（卷票）、增值税专用发票、增值税电子专用发票、增值税电子普通发票、
    // 货物运输业增值税专用发票、机动车销售统一发票、二手车销售统一发票、通用机打发票、通用定额发票、旅客运输普票、公路客运发票、船运客票、
    // 出租车发票、停车费发票、过路过桥费发票、教育费收据、行程单、火车票、增值税销货清单和其他可报销票据
    private static String billApi = "http://192.168.0.199:10001/cci_ai/service/v1/receipt_crop_and_recog";

    /**
     * true:使用私有化部署的api
     * false:使用官网api
     */
    private static Boolean localApi = null;
    private static Boolean headerByOrderPosition = null;


    public static void init(Application application) {
        TextInConfig.application = application;
    }

    protected static String getApiTable() {
        if (isLocalApi()) {
            return getTableApi();
        }
        return TEXTIN_API_TABLE;
    }

    protected static String getApiVatInvoice() {
        if (isLocalApi()) {
            return getVatInvoiceApi();
        }
        return TEXTIN_API_VAT_INVOICE;
    }


    protected static String getApiBusinessLicense() {
        if (isLocalApi()) {
            return getBusinessLicenseApi();
        }
        return TEXTIN_API_BUSINESS_LICENSE;
    }
    protected static String getApiBill() {
        if (isLocalApi()) {
            return getBillApi();
        }
        return TEXTIN_API_BILL;
    }

    public static String getX_ti_app_id() {
        if (x_ti_app_id == null) {
            x_ti_app_id = getSp().getString("x_ti_app_id", null);
        }
        return x_ti_app_id;
    }

    public static void setX_ti_app_id(String x_ti_app_id) {
        TextInConfig.x_ti_app_id = x_ti_app_id;
        getSp().edit().putString("x_ti_app_id", x_ti_app_id).apply();
    }

    public static String getX_ti_secret_code() {
        if (x_ti_secret_code == null) {
            x_ti_secret_code = getSp().getString("x_ti_secret_code", null);
        }
        return x_ti_secret_code;
    }

    public static void setX_ti_secret_code(String x_ti_secret_code) {
        TextInConfig.x_ti_secret_code = x_ti_secret_code;
        getSp().edit().putString("x_ti_secret_code", x_ti_secret_code).apply();
    }

    public static String getTableApi() {
        if (tableApi == null) {
            tableApi = getSp().getString("tableApi", null);
        }
        return tableApi;
    }

    public static void setTableApi(String tableApi) {
        TextInConfig.tableApi = tableApi;
        getSp().edit().putString("tableApi", tableApi).apply();
    }

    public static String getVatInvoiceApi() {
        if (vatInvoiceApi == null) {
            vatInvoiceApi = getSp().getString("vatInvoiceApi", null);
        }
        return vatInvoiceApi;
    }

    public static void setVatInvoiceApi(String vatInvoiceApi) {
        TextInConfig.vatInvoiceApi = vatInvoiceApi;
        getSp().edit().putString("vatInvoiceApi", vatInvoiceApi).apply();
    }

    public static String getBusinessLicenseApi() {
        if (businessLicenseApi == null) {
            businessLicenseApi = getSp().getString("businessLicenseApi", null);
        }
        return businessLicenseApi;
    }

    public static void setBusinessLicenseApi(String businessLicenseApi) {
        TextInConfig.businessLicenseApi = businessLicenseApi;
        getSp().edit().putString("businessLicenseApi", businessLicenseApi).apply();
    }

    public static String getBillApi() {
        if (billApi == null) {
            billApi = getSp().getString("billApi", null);
        }
        return billApi;
    }

    public static void setBillApi(String billApi) {
        TextInConfig.billApi = billApi;
        getSp().edit().putString("billApi", billApi).apply();
    }

    public static boolean isLocalApi() {
        if (localApi == null) {
            localApi = getSp().getBoolean("localApi", true);
        }
        return localApi;
    }

    public static void setLocalApi(boolean localApi) {
        TextInConfig.localApi = localApi;
        getSp().edit().putBoolean("localApi", localApi).apply();
    }


    public static Boolean getHeaderByOrderPosition() {
        if (headerByOrderPosition == null) {
            headerByOrderPosition = getSp().getBoolean("headerByOrderPosition", true);
        }
        return headerByOrderPosition;
    }

    public static void setHeaderByOrderPosition(Boolean headerByOrderPosition) {
        TextInConfig.headerByOrderPosition = headerByOrderPosition;
        getSp().edit().putBoolean("headerByOrderPosition", headerByOrderPosition).apply();
    }

    private static SharedPreferencesWrap getSp() {
        return SharedPreferencesWrap.with(application, "textin");
    }


}
